﻿using System.Linq;
namespace SimpleNow.Core.Authentication
{
    public class AuthentificationDAL
    {
        public void Login(User loginUser)
        {
            UserRecord myPlayerR = new UserRecord();
            using (var db = new AuthentificationEntities())
            {
                var query = from UserRecord in db.UserRecords
                            where UserRecord.Name == loginUser.Name
                            select UserRecord;


                foreach (var m in query)
                {

                    myPlayerR = m;

                }

            }
            SimpleNow.Core.Framework.Security mySecurity = new SimpleNow.Core.Framework.Security();
            if (myPlayerR.PasswordCrypted == mySecurity.MD5(loginUser.Name + loginUser.PasswordClear, myPlayerR.PasswordSalt))
            {
                int a = 1;
                a = 1 + 1;
            }

        }

        public void AddUser(User loginUser)
        {
            using (var db = new AuthentificationEntities())
            {
                SimpleNow.Core.Framework.Security mySecurity = new SimpleNow.Core.Framework.Security();
                UserRecord myUserRecord = new UserRecord();
                myUserRecord.Name = loginUser.Name;
                myUserRecord.Password = loginUser.PasswordClear;
                byte[] Salt = mySecurity.CreateRandomSaltBytes();
                myUserRecord.PasswordSalt = Salt;
                myUserRecord.PasswordCrypted = mySecurity.MD5(loginUser.Name + myUserRecord.Password, Salt);
                db.AddToUserRecords(myUserRecord);
                db.SaveChanges();


            }
        }
    }
}
